////
//
/// \file ./src/source_buffer.cpp
//
//  Copyright 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/source_buffer.hpp>

#include <stdint.h>

#include <algorithm>
#include <string>
#include <deque>

#include <boost/regex.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::regex;
using boost::match_results;

// Implement the most basic SourceBuffer constructor by copying a complete
// buffer of data.
SourceBuffer::SourceBuffer( const string& data_ )
    : data(data_.begin(), data_.end())
{}

// Basic matching against a fixed string of text.
bool
SourceBuffer::match_str( const string& text,
                         Iterator pos,
                         Iterator& end_pos )
{
  if( !equal( text.begin(), text.end(), pos ) )
    return false;

  end_pos = pos + text.size();
  return true;
}

// Match against a regular expression.
bool
SourceBuffer::match_re( const regex& re,
                        Iterator pos,
                        Iterator& end_pos )
{
  match_results< Iterator > m;
  if( regex_search( pos, end(), m, re, boost::match_continuous ) )
  {
    end_pos = m[0].second;
    return true;
  }

  return false;
}

} //  end inc namespace
} //  end parser namespace
